function QS_NumberFormatter(val,f,s,dec){
	if(typeof(QS_NumFmt)!="undefined"&&typeof(QS_NumFmt.Fmt2)=="function"){
		return QS_NumFmt.Fmt2(val,false,'',2);
	}
	if(typeof(dec)!="undefined"&&!isNaN(dec)){
		return parseFloat(val).toFixed(dec);
	}else{
		return val;
	}
}

QS_WorldMap=function(container,config,callback){
	this.container = container||{};
	this.config = $.extend(true,{showTitle:true,lang:{"americas":"Americas","greatereurope":"Greater Europe","greaterasia":"Greater Asia"}},config);
	this.callback = $.extend(true,{onLoad:function(){}},callback);
	this.BG=null;//Background object
	this.TP=null;//tip object
	this.tValidation = new tickerValidation(24);
	this.indexs ={
		"0P00001G7J":{pos:[36,56],name:"S&P 500"},
		"0P00001G7B":{pos:[52,70],name:"NASDAQ"},
		"0P00001FJG":{pos:[77,59],name:"DJIA"},
		"390:10:CO-IGBC":{pos:[76,102],name:"IGBC"},
		"398:10:PE-ISBVL":{pos:[63,125],name:"ISBVL"},
		"238:10:IBOVESPA":{pos:[102,126],name:"BVSP"},
		"237:10:MERVAL":{pos:[90,150],name:"MERV"},
		"0P00006K76":{pos:[73,159],name:"IPSA"},
		
		//"0P00001IS1":{pos:[163,35],name:"FTSE"},
		"0P00006G3M":{pos:[183,39],name:"DAX"},
		"0P00008ICX":{pos:[166,55],name:"CAC 40"},
		"211:10:PSI20":{pos:[149,62],name:"PSI 20"},
		"198:10:!IBEX":{pos:[157,57],name:"IBEX 35"},

		"141:10:D0_001":{pos:[298,65],name:"KOSPI"},//need check
		"0P00006NNM":{pos:[291,78],name:"SSEC"},
		"0P00001MUH":{pos:[322,155],name:"ASX Todos Ordinarios"}
		//"0P00001FL8":{pos:[288,90],name:"HSI"}
	};
	this.titleMap={
		"americas":{txt:this.config.lang["americas"],pos:[50,0],el:null,width:0},
		"greatereurope":{txt:this.config.lang["greatereurope"],pos:[190,0],el:null,width:0},
		"greaterasia":{txt:this.config.lang["greaterasia"],pos:[300,0],el:null,width:0}
	};
	this.gKeyList = [];
	this.titleHeight = 0;
	this._init();
};
QS_WorldMap.prototype={
	_init:function(){
		var self = this;
		
		if(this.config.showTitle){
			this.container.append(this._getTitleHtml());
			this._getTitleEl();
			this.titleHeight = this.container.find(".qs-worldmap-title").outerHeight(true);
		}
		this.BG=new QS_WorldMap.Background(this.container,{height:this.container.height()-this.titleHeight});
		if(this.config.showTitle){
			this._changeTitleElPosition();
			
		}
		this.TP =new QS_WorldMap.Tip(this.container,{marginTop:this.titleHeight},{getPositionByPrice:function(p){
			return self.BG.getPositionByPrice(p);
		}});
		this.TP.bulidHtml(this.indexs);	
		this._subscribeData();
		setTimeout(function(){
			self.changeOutSize(self.container.width(),self.container.height());
		},500);
		this.TP.flushAllPoints();
		if(typeof(this.callback.onLoad)=="function"){
			this.callback.onLoad({compontent:"worldmap",height:this.container.height(),width:this.container.width()});
		}
		
	},
	_getTitleHtml:function(){
		var str ="<div class='qs-worldmap-title'>";
		for(var i in this.titleMap){
			str +="<span class='qs-worldmap-title-"+i+"'>"+this.titleMap[i]["txt"]+"</span>";
		}
		str +="</div>";
		return str;
	},
	_getTitleEl:function(){
		for(var i in this.titleMap){
			this.titleMap[i]["el"]=this.container.find(".qs-worldmap-title span.qs-worldmap-title-"+i);
			this.titleMap[i]["width"]=this.titleMap[i]["el"].width()/2;
		}
	},
	_changeTitleElPosition:function(){
		for(var i in this.titleMap){
			this.titleMap[i]["el"].css({left:this.BG.getPositionByPrice(this.titleMap[i]["pos"])[0]-this.titleMap[i]["width"]});
		
		}
	},
	
	changeOutSize:function(w,h){
		var parent = this.container.parent();
        w = w||parent.width();
        h = h||parent.height();
		if(!w||!h){
			return;
		}
		this.BG.changeOutSize(w,h-this.titleHeight);
		this.TP.flushPosition(this.indexs);
		if(this.config.showTitle){
			this._changeTitleElPosition();
		}
	},
	_subscribeData:function(){
		for(var i in this.indexs){
			this.gKeyList.push(i);
		}
		var self = this;
		this.tValidation.checkTickers(this.gKeyList,function(){
			self.TP.flushAllPoints();
		},true);
		
	},
	_unSubscribe:function(tickers,flag){
		this.tValidation.unSubscribe(tickers,flag);
	},
	updateData:function(gKey,data){
        if(!data){
            return false;
        }  
		this.TP.flushPointsByPid(gKey);
     },
	clear:function(){
		
	}
};

QS_WorldMap.Tip=function(container,config,callMethod){
	this.container = container||{};
	this.config = $.extend(true,{marginTop:0},config);
	this.callMethod=callMethod;
	this.tipPid = "";
	this.tipEl = null;
	this.tipElChgEl = null;
	this.pointElsMap = {};
};
QS_WorldMap.Tip.prototype={

	bulidHtml:function(indexs){
		var tempHtml = "<span class='qs-chart-worldmap-tip'><span class='qs-chart-worldmap-tip-updown'></span></span>";
		for(var i in indexs){
			var pos =this.callMethod["getPositionByPrice"](indexs[i]["pos"]);
			tempHtml +="<div class='qs-chart-worldmap-point' data-pid='"+i+"' style='left:"+pos[0]+"px;top:"+(pos[1]+this.config.marginTop)+"px;'></div>";
		}
		this.container.append(tempHtml);
		this._bindEvent(indexs);
		
		var pointEls = this.container.find(".qs-chart-worldmap-point");
		//this.pointEls = pointEls;
		for(var i =0;i<pointEls.length;i++){
			this.pointElsMap[pointEls.eq(i).attr("data-pid")]=pointEls.eq(i);
		}
		this.flushAllPoints();
	},
	flushPosition:function(indexs){
		for(var i in indexs){
			var pos =this.callMethod["getPositionByPrice"](indexs[i]["pos"]);
			this.pointElsMap[i].css({left:pos[0],top:pos[1]+this.config.marginTop});
		}
		var target =this.pointElsMap[this.tipPid];
		if(target){
			this._changeTipPostion(target);
		}
		
	},
	flushAllPoints:function(){
		for(var i in this.pointElsMap){
			this._flushPointsEl(this.pointElsMap[i],i);
		}
	},
	flushPointsByPid:function(gkey){
		var pid = gData[gkey]["performanceId"];
		this._flushPointsEl(this.pointElsMap[pid],pid);
	},
	_flushPointsEl:function(el,pid){
		if(gIdMap[pid]){
			var chg = gData[gIdMap[pid]]["Chg%"];
			if(typeof(chg)=="undefined"||!el){
				return ;
			}
			
			if(chg>=0){
				if(!el.hasClass("qs-chart-worldmap-point-up")){
					el.addClass("qs-chart-worldmap-point-up");
				}
			}else{
				el.removeClass("qs-chart-worldmap-point-up");
			}
			if(this.tipPid==pid&&this.tipElChgEl!=null&&!isNaN(chg)){
				chg = QS_NumberFormatter(chg,false,'',2);
				this.tipElChgEl.html(chg);
			}
		}
	},
	_changeTipPostion:function(target,tipElWrap){
		var left = target.position().left+15,top = target.position().top-10;
		if((left+15+this.tipEl.outerWidth(true))>this.container.width()){
			left = target.position().left-this.tipEl.outerWidth(true)-5;
		}
		this.tipEl.animate({left:left,top:top},100);
	},
	_hideTip:function(){
		this.tipEl.hide();
	},
	_bindEvent:function(indexs){
	var self =this;
		$(document).bind("mousedown.worldmap",function(e){
			var target = $(e.target);
			if(!target.hasClass("qs-chart-worldmap-point")){
				self._hideTip();
			}
		}).bind("mouseleave.worldmap",function(e){
			self._hideTip();
		})
		var tipEl = this.container.find(".qs-chart-worldmap-tip span"),tipElWrap = this.container.find(".qs-chart-worldmap-tip");
		this.tipEl = tipElWrap;
		tipElWrap.hide();
		
		this.container.find(".qs-chart-worldmap-point").bind("mouseover",function(e){
			var target = $(e.target);
			tipElWrap.show();
			var pid = target.attr("data-pid");
			self.tipPid = pid;
			var chg ="--";
			if(gIdMap[pid]){
				chg = gData[gIdMap[pid]]["Chg%"];
				if(typeof(chg)!="undefined"){
					//chg = (chg).toFixed(2);
				}else{
					chg ="--";
				}
			}
			tipEl.html(indexs[pid]["name"]+":<span>"+QS_NumberFormatter(chg,false,'',2)+"</span>%");
			self.tipElChgEl = tipEl.find("span");
			if(chg>=0){
				if(!tipEl.hasClass("qs-chart-worldmap-tip-up")){
					tipEl.addClass("qs-chart-worldmap-tip-up");
				}
			}else{
				tipEl.removeClass("qs-chart-worldmap-tip-up");
			}
			self._changeTipPostion(target);
		});;
	}
};

QS_WorldMap.Background=function(container,config){
	this.container = container||{};
	this.config = $.extend(true,{height:this.container.height()},config);
	this.canvasObj = null;
	this.gcList=["_getWorldMapData"];
	this.init();
};

QS_WorldMap.Background.prototype={
	init:function(){
		if(typeof(chartCanvas)!="function"){
			throw new Error("No canvas. Please import javascript file.");
		}
		this.canvasObj = new chartCanvas(this.container,{color:{line:"#c3c3c3"},worldmap:true,canvasHeight:this.config.height});
		this.canvasObj.drawWorldMap(this._getWorldMapData());
		this._GC();
	},
	changeOutSize:function(w,h){
		this.canvasObj.changeOutSize(w,h);
	},
	getPositionByPrice:function(p){
		return this.canvasObj.getPositionByPrice(p);
	},
	_getWorldMapData:function(){
		return {NorthAmerica:[[42,0],[77,0],[73,4],[73,6],[91,24],[82,32],[98,47],[98,50],[97,52],[95,52],[88,60],[81,60],[81,68],[73,75],[73,82],[71,84],[69,84],[69,79],[67,76],[58,76],[58,88],[46,76],[35,76],[30,69],[30,52],[18,39],[10,48],[6,48],[6,43],[10,39],[0,30],[0,15],[7,8],[34,8]],NorthAmericaSpace1:[[62,14],[77,14],[77,16],[70,22],[70,40],[56,27],[56,24],[62,18]],NorthAmericaSpace2:[[65,47],[67,47],[77,58],[71,63],[71,58],[70,58],[70,54],[59,54]],SourceAmerica:[[30,69],[35,76],[46,76],[58,88],[58,93],[62,93],[66,88],[66,98],[76,98],[78,96],[88,96],[113,121],[113,137],[99,150],[99,154],[83,167],[83,175],[79,175],[79,188],[74,188],[68,182],[68,159],[72,156],[72,138],[64,130],[64,113],[72,104],[57,104],[31,78]],America:[[70,85],[78,85],[78,89],[70,89]],UK:[[165,28],[165,43],[158,36]],WestEuro:[[186,10],[210,10],[217,18],[164,71],[162,71],[154,62],[154,59],[176,36],[168,29],[168,27]],WestEuro1:[[158,36],[158,40],[154,40]],EmergingEuro:[[229,5],[231,5],[240,14],[240,71],[198,71],[188,62],[186,62],[180,69],[178,69],[172,63]],EmergingEuroSpace1:[[226,52],[228,52],[232,56],[228,59],[228,70],[223,66],[223,56]],Africa:[[162,71],[240,71],[240,84],[235,84],[217,102],[217,104],[219,105],[219,107],[209,116],[209,134],[200,142],[200,150],[181,169],[179,169],[179,126],[168,114],[156,114],[149,107],[149,84]],AfricaSpace1:[[198,79],[216,96],[216,101],[198,84]],Africa1:[[213,135],[226,135],[215,147],[213,147]],AfricaSpace2:[[173,63],[178,69],[180,69],[187,62],[198,71],[164,71]],Japan:[[332,55],[334,55],[334,67],[324,77],[312,77],[312,75]],Australia:[[297,129],[319,129],[330,140],[330,162],[318,173],[308,162],[281,162],[281,145]],Australia1:[[345,155],[347,155],[347,167],[340,174],[337,174],[332,168]],Tiger1:[[299,73],[304,73],[304,78]],Tiger2:[[303,85],[303,91],[297,91]],Tiger3:[[295,90],[290,90],[290,95]],Tiger4:[[273,109],[279,109],[279,114],[277,114]],EmergingAsia:[[240,82],[278,44],[322,44],[322,46],[304,63],[304,73],[299,73],[295,70],[295,90],[290,90],[285,90],[285,91],[279,91],[285,97],[285,99],[275,109],[272,109],[260,97],[240,117]],EmergingAsia1:[[279,115],[295,115],[288,124]],EmergingAsia2:[[295,116],[309,101],[309,116]],EmergingAsia3:[[310,115],[333,115],[322,126],[320,126]],Asia:[[254,0],[296,0],[303,8],[321,8],[328,16],[357,16],[357,24],[335,44],[333,44],[320,32],[318,32],[315,35],[315,37],[321,44],[278,44],[240,82],[240,14]]};
		//return [[[42,0],[77,0],[73,4],[73,6],[91,24],[82,32],[98,47],[98,50],[97,52],[95,52],[88,60],[81,60],[81,68],[73,75],[73,82],[71,84],[69,84],[69,79],[67,76],[58,76],[58,88],[46,76],[35,76],[30,69],[30,52],[18,39],[10,48],[6,48],[6,43],[10,39],[0,30],[0,15],[7,8],[34,8]],[[62,14],[77,14],[77,16],[70,22],[70,40],[56,27],[56,24],[62,18]],[[30,69],[35,76],[46,76],[58,88],[58,93],[62,93],[66,88],[66,98],[76,98],[78,96],[88,96],[113,121],[113,137],[99,150],[99,154],[83,167],[83,175],[79,175],[79,188],[74,188],[68,182],[68,159],[72,156],[72,138],[64,130],[64,113],[72,104],[57,104],[31,78]],[[70,85],[78,85],[78,89],[70,89]],[[165,28],[165,43],[158,36]],[[186,10],[210,10],[217,18],[164,71],[162,71],[154,62],[154,59],[176,36],[168,29],[168,27]],[[158,36],[158,40],[154,40]],[[229,5],[231,5],[240,14],[240,71],[198,71],[188,62],[186,62],[180,69],[178,69],[172,63]],[[226,52],[228,52],[232,56],[228,59],[228,70],[223,66],[223,56]],[[162,71],[240,71],[240,84],[235,84],[217,102],[217,104],[219,105],[219,107],[209,116],[209,134],[200,142],[200,150],[181,169],[179,169],[179,126],[168,114],[156,114],[149,107],[149,84]],[[198,79],[216,96],[216,101],[198,84]],[[213,135],[226,135],[215,147],[213,147]],[[173,63],[178,69],[180,69],[187,62],[198,71],[164,71]],[[332,55],[334,55],[334,67],[324,77],[312,77],[312,75]],[[297,129],[319,129],[330,140],[330,162],[318,173],[308,162],[281,162],[281,145]],[[345,155],[347,155],[347,167],[340,174],[337,174],[332,168]],[[240,82],[278,44],[322,44],[322,46],[304,63],[304,73],[299,73],[295,70],[295,90],[290,90],[285,90],[285,91],[279,91],[285,97],[285,99],[275,109],[272,109],[260,97],[240,117]],[[299,73],[304,73],[304,78]],[[303,85],[303,91],[297,91]],[[295,90],[290,90],[290,95]],[[273,109],[279,109],[279,114],[277,114]],[[279,115],[295,115],[288,124]],[[295,116],[309,101],[309,116]],[[310,115],[333,115],[322,126],[320,126]],[[254,0],[296,0],[303,8],[321,8],[328,16],[357,16],[357,24],[335,44],[333,44],[320,32],[318,32],[315,35],[315,37],[321,44],[278,44],[240,82],[240,14]]];
	},
	_GC:function(){
		for(var i =0;i<this.gcList.length;i++){
			//delete this.gcList[i];
		}
	}
}